package com.yihaodian.server.servlet.rest;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yihaodian.api.common.Response;
import com.yihaodian.api.search.SugRequest;

/**
 * 
 * http://127.0.0.1:8080/search/rest/sug
 * 
 */
public class SugServlet extends AbstractServlet {
	public static final String param_query = "q";
	public static final String param_field = "field";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String index = super.getIndexName(req);

		String key = req.getParameter(param_query);
		String field = req.getParameter(param_field);

		SugRequest request = new SugRequest();
		request.setKey(key);
		request.setField(field);
		request.setIndexName(index);
		Response response = super.brokerService.execute(request);

		super.respJson(req, resp, response);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void init() throws ServletException {
		super.init();
	}
}
